home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / comm / www / StreamMP3.lha / StreamMP3.rx < prev   
Text File  |  1999-04-28  |  9KB  |  289 lines

  1. /* StreamMP3.rx by Sigbjørn (CISC) Skjæret
  2. ** $VER: StreamMP3.rx v2.3 (28.04.99)
  3. **
  4. ** Tries to stream a MP3 file via a file containing URL(s) (like a .m3u or .pls file)...
  5. */
  6. options results
  7. options failat 21
  8. signal on halt
  9. signal on break_c
  10. parse arg args
  11.  
  12.  
  13. /*** Edit these options ***/
  14.  
  15. mp3prog        = 'C:MPEGA'                            /* Which decoder to run */
  16. mp3args        = '-m -t2000 -F22050 -I0002000C'    /* Commandline arguments to the mp3 prog */
  17.  
  18. rxprog        = 'SYS:RexxC/RX'                    /* The ARexx interpreter */
  19.  
  20. bfsize        = '16384'                            /* Initial playback buffer (will revert to "pbsize" variable after first read) */
  21. pbsize        = '1024'                            /* Playback buffer (keep this low unless you have a very fast connection to the stream) */
  22.  
  23. save        = '0'                                /* Save file while streaming? (1 for yes, 0 for no) */
  24. savepath    = 'RAM:'                            /* Where to save the file while streaming */
  25. comment        = '1'                                /* Save URL as comment to the file saved? (1 for yes, 0 for no) */
  26.  
  27. reqtools    = '1'                                /* Display ReqTools requester? (1 for yes, 0 for no) */
  28. pubscreen    = 'Workbench'                        /* The public screen you want the requester on */
  29.  
  30. /*** <-end-> ***/
  31.  
  32.  
  33. if ~show('L','rxsocket.library') then
  34. do
  35.  if ~addlib('rxsocket.library',0,-30,0) then
  36.  do
  37.   say 'ARexx socket library not available, exiting'
  38.   exit 10
  39.  end
  40. end
  41.  
  42. if(reqtools) then
  43.  if ~show('L','rexxreqtools.library') then
  44.   if ~addlib('rexxreqtools.library',0,-30,0) then reqtools=0
  45.  
  46. if(~exists(mp3prog) | ~exists(rxprog)) then
  47. do
  48.  if ~exists(rxprog) then notfound=rxprog
  49.  else notfound=mp3prog
  50.  
  51.  say 'Sorry, "'notfound'" does not exist, please edit path in script.'
  52.  exit 10
  53. end
  54.  
  55. if(upper(word(args,1))='URL') then
  56. do
  57.  url=word(args,2)
  58.  mp3tmp=word(args,3)
  59.  call Stream()
  60.  
  61.  call close(pipe)
  62.  call CloseSocket(sock)
  63.  if(save) then
  64.  do
  65.   call close(file)
  66.   if(comment) then
  67.    if(shoutcast & name~=host & name~='') then address command 'FileNote "'savepath''filename''app'" "'name'"'
  68.    else address command 'FileNote "'savepath''filename''app'" http://'url
  69.  end
  70.  
  71.  exit
  72. end
  73. else if(upper(word(args,1))='BREAK') then
  74. do
  75.  pidtmp='pid'time('S')
  76.  address command 'Status >T:'pidtmp
  77.  call open(pid,'T:'pidtmp,R)
  78.  do while ~eof(pid)
  79.   line=readln(pid)
  80.   if(pos(mp3prog,line)>0) then
  81.   do
  82.    id=strip(word(line,2),'T',':');cmd=subword(line,6)
  83.    address command 'Break >NIL: 'id
  84.    say 'Sent Ctrl-C to process 'id': 'cmd
  85.   end
  86.  end
  87.  call close(pid)
  88.  address command 'Delete >NIL: T:'pidtmp' QUIET'
  89.  say 'Done.'
  90.  exit
  91. end
  92.  
  93. args=strip(args,'B','"')
  94.  
  95. parse source . . . pname;pname=subword(pname,1,words(pname)-2);found=0
  96.  
  97. if(pos(' ',pname)>0) then
  98. do
  99.  say 'Sorry, due to restrictions in RX, you''ll have to place "'pname'" somewhere where the path does not resolve with a space in it.'
  100.  exit 10
  101. end
  102.  
  103. if(args='') then
  104. do
  105.  if(reqtools) then url=rtgetstring(,'Enter URL:','Enter URL')
  106.  else do;say 'Enter URL:';parse pull url;end
  107.  
  108.  if(url='') then
  109.  do
  110.   say 'No URL entered .. exiting!'
  111.   exit
  112.  end
  113.  
  114.  if(pos('://',url)=0) then url='http://'url
  115.  call Play()
  116. end
  117. else if ~open(file,args,R) then
  118. do
  119.  say 'Unable to open file containing URL!!!'
  120.  exit 10
  121. end
  122. else do x=1 while ~eof(file)
  123.  url=readln(file);url=strip(strip(url,'T',d2c(13)),'B')
  124.  if(url='') then iterate x
  125.  
  126.  if(upper(left(url,4))='FILE') then url=substr(url,pos('=',url)+1)
  127.  
  128.  if(~abbrev(url,'http://',7)) then iterate x
  129.  
  130.  call Play()
  131. end
  132.  
  133. if ~found then
  134. do
  135.  say 'No supported URLs found!!!'
  136.  exit 10
  137. end
  138.  
  139. exit
  140.  
  141.  
  142. /*** PROCEDURES ***/
  143.  
  144. Play:
  145.  if(upper(right(url,4))='.M3U' | upper(right(url,4))='.PLS') then
  146.  do
  147.   if(pos('http://',url)=1) then url=substr(url,8);port='80';crlf=d2c(13)''d2c(10);lookup=1
  148.   if(pos('/',url)>0) then do;host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url));end
  149.   else do;host=url;path='/';end
  150.   if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end
  151.  
  152.   if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;say 'Unable to lookup host!!!';iterate x;end
  153.   sock=Socket('INET','STREAM','IP');if sock<0 then do;say 'Unable to create socket!!!';iterate x;end
  154.   conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0
  155.   if Connect(sock,'CONN')<0 then do;if(lookup) then say 'Sorry, server is down, unable to stream data!';else say 'Host does not exist!!!';iterate x;end
  156.  
  157.   req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf
  158.   call Send(sock,req)
  159.  
  160.   len=1
  161.   do y=1 while len>0
  162.    len=recvline(sock,'BUF',1024)
  163.    if(buf=crlf) then
  164.    do z=1 while len>0
  165.     len=recvline(sock,'BUF',1024)
  166.     url=strip(strip(strip(buf,'T',d2c(10)),'T',d2c(13)),'B')
  167.     if(upper(left(url,4))='FILE') then url=substr(url,pos('=',url)+1)
  168.     if(url='') then iterate z
  169.     if(~abbrev(url,'http://',7)) then iterate z
  170.     leave y
  171.    end
  172.   end
  173.   call CloseSocket(sock)
  174.  end
  175.  
  176.  if(url='') then return 0
  177.  
  178.  mp3tmp='mp3'time('S');setclip(mp3tmp);found=1
  179.  address command 'Run >NIL: 'rxprog' 'pname' URL 'url' 'mp3tmp
  180.  
  181.  do i=1 to 15
  182.   address command 'Wait 5'
  183.   clip=getclip(mp3tmp)
  184.   if(clip='PLAY' | clip='SAVE' | clip='NOSAVE') then leave i
  185.   if(clip~='') then
  186.   do
  187.    say 'Failed: 'clip
  188.    setclip(mp3tmp)
  189.    return 0
  190.   end
  191.   if(i=15) then
  192.   do
  193.    say 'Operation timed out...'
  194.    setclip(mp3tmp,'DIE') /* Just in case */
  195.    return 0
  196.   end
  197.  end
  198.  
  199.  address command ''mp3prog' >NIL: 'mp3args' -r -n -b0 PIPE:'mp3tmp
  200.  setclip(mp3tmp,'DIE')
  201. return 0
  202.  
  203. Stream:
  204.  filename='';size='';name='';genre='';bitr='';shoutcast=0;app='';num=0
  205.  if(pos('http://',url)=1) then url=substr(url,8);port='80'
  206.  if(pos('/',url)>0) then do;host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url));end
  207.  else do;host=url;path='/';end
  208.  if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end
  209.  
  210.  if(savepath~='') then
  211.  do
  212.   if(right(savepath,1)~=':' & right(savepath,1)~='/') then savepath=savepath'/'
  213.   if(path='/') then filename=mp3tmp'.mp3'
  214.   else filename=substr(path,lastpos('/',path)+1)
  215.  end
  216.  
  217.  crlf=d2c(13)''d2c(10);lookup=1
  218.  
  219.  if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;setclip(mp3tmp,'Unable to lookup host!!!');return 10;end
  220.  sock=Socket('INET','STREAM','IP');if sock<0 then do;setclip(mp3tmp,'Unable to create socket!!!');return 10;end
  221.  conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0
  222.  if Connect(sock,'CONN')<0 then do;if(lookup) then setclip(mp3tmp,'Sorry, server is down, unable to stream data!');else setclip(mp3tmp,'Host does not exist!!!');return 10;end
  223.  
  224.  req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf
  225.  call Send(sock,req)
  226.  
  227.  call open(pipe,'PIPE:'mp3tmp,W)
  228.  if(save) then do;call open(file,savepath''filename,W);num=1;end
  229.  
  230.  len=1;start=0
  231.  do y=1 while len>0
  232.   if(start) then
  233.   do
  234.    len=recv(sock,'BUF',bfsize);bfsize=pbsize
  235.    if(len>0) then
  236.    do
  237.     call writech(pipe,buf)
  238.     if(save) then call writech(file,buf)
  239.    end
  240.    clip=getclip(mp3tmp)
  241.    if(clip='DIE') then do;setclip(mp3tmp);return 0;end
  242.    if(clip='SAVE' & save=0) then do;save=1;num=num+1;if(num>1) then app='.'num;call open(file,savepath''filename''app,W);call writech(file,buf);end
  243.    if(clip='NOSAVE' & save=1) then do;save=0;call close(file);if(comment) then if(shoutcast & name~=host & name~='') then address command 'FileNote "'savepath''filename''app'" "'name'"';else address command 'FileNote "'savepath''filename''app'" http://'url;end
  244.   end
  245.   else
  246.   do
  247.    len=recvline(sock,'BUF',1024)
  248.    if(y=1 & word(buf,1)='ICY') then shoutcast=1
  249.    if(y=1 & word(buf,2)~='200') then do;setclip(mp3tmp,strip(subword(buf,3),'T',crlf));return 10;end
  250.    if(word(buf,1)='Content-Length:') then size=strip(word(buf,2),'T',crlf)
  251.    if(left(buf,9)='icy-name:') then name=compress(strip(substr(buf,10),'T',crlf),"'""")
  252.    if(left(buf,10)='icy-genre:') then genre=compress(strip(substr(buf,11),'T',crlf),"'""")
  253.    if(left(buf,7)='icy-br:') then bitr=strip(substr(buf,8),'T',crlf)
  254.    if(buf=crlf) then do;start=1;setclip(mp3tmp,'PLAY');call OpenReq();end
  255.   end
  256.  end
  257. return 0
  258.  
  259. OpenReq:
  260.  if(reqtools) then
  261.  do
  262.   lf=d2c(10)
  263.   stbt="'Start _Save'"
  264.   spbt="'Stop _Save'"
  265.   flsv="'"savepath""filename"'"
  266.  
  267.   if(size='') then size='Unknown'
  268.   else size=size' bytes'
  269.   if(save=0) then do;s